import { Request, Response } from 'express';
import moment from 'moment';

const ReservationList: {
  id: number;
  order_id: string;
  zone_name: string;
  space_name: string;
  plate_number: string;
  phone: string;
  apply_time: Date;
  reserve_fee: number;
  back_pay: number;
  pay_time: Date;
  actual_arrival_time: Date;
  actual_leave_time: Date;
  arrival_time: Date;
  leave_time: Date;
  parking_duration: string;
  actual_pay: number;
  amount_paid: number;
  status: number;
}[] = [];

function getReservationList(req: Request, res: Response) {
  console.log('请求到模拟数据了');
  for (let i = 1; i < 10; i++) {
    ReservationList.push({
      id: i,
      order_id: '20220621' + i,
      zone_name: '让你停测试车场' + i,
      space_name: 'a00' + i,
      plate_number: '浙A6666' + i,
      phone: '1972067765' + i,
      apply_time: moment().add(i, 'minutes').toDate(),
      reserve_fee: i,
      back_pay: i % 2 == 0 ? i : 0,
      pay_time: moment().add(i, 'hours').toDate(),
      actual_arrival_time: moment().add(i, 'hours').toDate(),
      actual_leave_time: moment()
        .add(i + 1, 'hours')
        .toDate(),
      arrival_time: moment()
        .add(i + 30, 'minutes')
        .toDate(),
      leave_time: moment()
        .add(i + 30, 'minutes')
        .toDate(),
      parking_duration: '1小时' + i + '分钟',
      actual_pay: i + 5,
      amount_paid: i,
      status: Math.floor(Math.random() * 5 + 1),
    });
  }
  res.json({
    code: 0,
    message: 'success',
    data: { list: ReservationList, total: 10 },
  });
}

function queryReservationDetail(req: Request, res: Response) {
  // console.log('ReservationList', ReservationList);

  const { id } = req.query;

  console.log('id', id);
  // const ReservationListItem = ReservationList.find((v) => v.id === Number(id));
  const ReservationListItem = ReservationList.filter((v) => v.id === Number(id));

  res.json({
    code: 0,
    message: 'success',
    data: { list: ReservationListItem[0] },
  });
}

export default {
  'GET /api/ReservationOrder/list': getReservationList,
  'GET /api/ReservationOrder/details': queryReservationDetail,
};
